//License: Artistic License v2.0, http://opensource.org/licenses/Artistic-2.0

package main

import (
	"fmt"
	"io/ioutil"
	"os"
)

func initProj(proj, theme, lang string) string {
	projName = proj
	if _, err := os.Open(path("proj")); err == nil {
		return "Err: util initProj() - project name already exists"
	}
	if err := Cp(path("seedProj"), path("proj")); err != nil {
		return fmt.Sprintln(err)
	}
	if err := Cp(path("seedTheme")+theme, path("themeDir")+theme); err != nil {
		return fmt.Sprintln(err)
	}
	if lang == "" {
		lang = "en"
	}
	if err := readConfigFile(path("configFile")); err != nil {
		return fmt.Sprintln(err)
	}
	conf.DefaultLang = lang
	if err := Json("set", path("configFile"), &conf); err != nil {
		return fmt.Sprintln(err)
	}
	return "Ok"
}

func setTheme(proj, theme string) string {
	projName = proj
	if _, err := os.Open(path("themeDir") + theme); err != nil {
		if err := Cp(path("seedTheme")+theme, path("themeDir")+theme); err != nil {
			return fmt.Sprintln(err)
		}
	}
	if err := readConfigFile(path("configFile")); err != nil {
		return fmt.Sprintln(err)
	}
	conf.Theme = theme
	if err := Json("set", path("configFile"), &conf); err != nil {
		return fmt.Sprintln(err)
	}
	return "Ok"
}

func addCopy(proj, copyId, syntax string) string {
	if !(syntax == "html" || syntax == "md") {
		return "Please specify html or md"
	}
	projName = proj
	var metaFile string = path("meta") + copyId + ".json"
	if _, err := os.Open(metaFile); err == nil {
		return "Err: util addCopy() - copy id already exists"
	}
	var copyFile string = path("copy") + copyId + "." + syntax

	if err := readConfigFile(path("configFile")); err != nil {
		return fmt.Sprintln(err)
	}
	var meta Copy
	meta.Lang = conf.DefaultLang
	meta.Cid = copyId
	fmt.Println("adding", metaFile)
	fmt.Sprintln(Json("set", metaFile, &meta))
	fmt.Println("adding", copyFile)
	if err := ioutil.WriteFile(copyFile, nil, 0600); err != nil {
		return fmt.Sprintln(err)
	}
	return "ok"
}

func rmCopy(proj, copyId string) string {
	projName = proj
	var metaFile string = path("meta") + copyId + ".json"
	var copyFile string = path("copy") + copyId + "."
	if err := Rm(metaFile); err != nil {
		return fmt.Sprintln(err)
	}
	if err := Rm(copyFile + "html"); err != nil {
		if err = Rm(copyFile + "md"); err != nil {
			return fmt.Sprintln(err)
		}
	}
	return "ok"
}

func manageUser(directive, userId, password string) string {
	var user struct { //struct of the user.json file for each user
		PasswordKey  string
		PasswordHash []byte
	}
	var userFile string = path("user") + userId + ".json"
	switch directive {
	case "set":
		{
			user.PasswordKey, user.PasswordHash = EncodeString(password)
			fmt.Sprintln(Json("set", userFile, &user))
		}
	case "test":
		{
			fmt.Sprintln(Json("get", userFile, &user))
			if !EncodingIsOk(password, user.PasswordKey, user.PasswordHash) {
				return fmt.Sprintln("Password for", userId, "is not", password)
			}
		}
	}
	return "Ok"
}

func showTextFile(fname string) string {
	str, err := ioutil.ReadFile(fname)
	if err != nil {
		return fmt.Sprintln(err)
	}
	return string(str)
}

//Copyright: Slamet Hendry